package com.ecube.test;

import com.alibaba.fastjson.JSON;
import com.ecube.mqtt.MqttUtils;
import com.ecube.protocol.ProtocolHelper;
import com.ecube.protocol.charge.ChargeChargerState;
import com.ecube.protocol.common.DataReportHead;
import com.ecube.protocol.common.DataReportStandard;
import com.ecube.protocol.enums.ChargeFunctionEnum;
import com.ecube.utils.DateUtils;
import com.ecube.utils.EnumUtils;
import lombok.Builder;
import lombok.Data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @author zhuweijun
 * @version 1.0.0
 * @since 2025/8/11 12:34
 */
public class SendChargeChargerState {


    public static void main(String[] args) throws Exception {

        while (true){
            sendChargerState("FAT", "100110001", "1001100012025155", ChargerInfo.builder().code("1001100012025155").gunSize(2).build());
            sendChargerState("FAT", "100110001", "1001100012025152", ChargerInfo.builder().code("1001100012025152").gunSize(2).build());

            Thread.sleep(1000 * 60 * 5);
        }
    }

    /**
     * 设备实时状态
     *
     * @param env env
     * @param productCode productCode
     * @param deviceCode deviceCode
     */
    private static void sendChargerState(String env, String productCode, String deviceCode, ChargerInfo info) {
        ChargeFunctionEnum function = (ChargeFunctionEnum) EnumUtils.getFunction(ChargeChargerState.class);
        if(function == null){
            return;
        }
        String pubTopic = productCode + "/" + deviceCode + "/" + function.getDirection() + "/" + function.getType();

        DataReportStandard reportStandard = new DataReportStandard();
        // 设置报文头
        DataReportHead reportHand = ProtocolHelper.getReportHand(productCode, deviceCode, function.getFunction());
        reportHand.setVersion("V2.0.0");
        reportStandard.setHeader(reportHand);

        ChargeChargerState chargerState = new ChargeChargerState();
        chargerState.setCode(info.getCode());

        // 充电接口列表
        List<ChargeChargerState.GunInfo> gunList = new ArrayList<>();
        for (Integer i = 1; i <= info.getGunSize(); i++) {
            ChargeChargerState.GunInfo gunInfo = new ChargeChargerState.GunInfo();
            gunInfo.setCode(i);
            gunList.add(gunInfo);
        }
        chargerState.setGunList(gunList);

        // 电表列表接口
        List<ChargeChargerState.MeterInfo> meterList = new ArrayList<>();
        ChargeChargerState.MeterInfo meterInfo = new ChargeChargerState.MeterInfo();
        meterInfo.setCode(info.getCode() + "001");
        meterInfo.setSubDeviceCode(info.getCode());
        meterInfo.setTotalEPI(Arrays.asList(1530.00F));
        meterInfo.setCTime(DateUtils.getNowDate());
        meterList.add(meterInfo);
        chargerState.setMeterList(meterList);

        reportStandard.setDataBody(chargerState);
        String reportContent = JSON.toJSONString(reportStandard);
        MqttUtils.sendMQTT(env, pubTopic, reportContent);
    }

    /**
     * 充放电设备
     */
    @Data
    @Builder
    private static class ChargerInfo {

        /**
         * 设备编号
         */
        private String code;

        /**
         * 充电枪数量
         */
        private Integer gunSize;
    }
}
